************************************************************
* Create TeX results table for treatment effect
* heterogeneity by predicted dosage
************************************************************
clear all
set more off

// Install packages
ssc install gtools, replace
ssc install mmerge, replace

// Read in command line arguments
args dosage_output dosage_output_dir output


* Control means by LOO group
use "`dosage_output'", clear
keep if treatment==0
gcollapse (mean) index ashoot aother vshoot, by(estrat_loo_group) fast
rename estrat_loo_group group
rename (index-vshoot) CM_=
reshape long CM_, i(group) j(outcome) string
rename CM_ CM
tempfile temp_CMs
save `temp_CMs', replace

* Take-up rates among treatment group by LOO group
use "`dosage_output'", clear
keep if treatment==1
gcollapse (mean) takeup=t_headcount_post_20 dosage=t_payroll_total_hours_post_20, by(estrat_loo_group) fast
replace takeup=takeup*100
rename estrat_loo_group group
rename (takeup dosage) var_=
reshape long var_, i(group) j(outcome) string
rename var_ var
tempfile temp_takeup
save `temp_takeup', replace

* N by LOO group
use "`dosage_output'", clear
gcollapse (count) var=cluster, by(estrat_loo_group) fast
rename estrat_loo_group group
gen outcome="N"
tempfile temp_N
save `temp_N', replace

* Load outcome-specific estimates
tempfile temp
cap erase `temp'
foreach k in index ashoot aother vshoot {
	use "`dosage_output_dir'dosage_heterogeneity_`k'.dta", clear
	cap append using `temp'
	save `temp', replace
}

* Combine control means, take-up rates, Ns, and outcome-specific estimates
mmerge outcome group using `temp_CMs', t(1:1) unmatched(master)
drop _merge
gsort +outcome +group
append using `temp_takeup'
append using `temp_N'

* Generate p-values from z-statistics
gen LOO_p=2*(1-normal(abs(LOO_B/LOO_SE)))
gen RSS_p=2*(1-normal(abs(RSS_B/RSS_SE)))

* Generate TeX table
cap file close _all
file open table using "`output'", write replace

file write table "\addlinespace[0.3em]" _n "\multicolumn{8}{l}{\textbf{Primary Index of Serious Violence}}\\" _n
foreach k in index {
	file write table "\hspace{1em}"
	foreach g in 1 2 {
	foreach z in CM LOO_B RSS_B {
		sum `z' if outcome=="`k'" & group==`g'
		file write table " & " (string(r(mean),"%15.4fc"))
		if "`z'"=="LOO_B" sum LOO_p if outcome=="`k'" & group==`g'
		if "`z'"=="RSS_B" sum RSS_p if outcome=="`k'" & group==`g'
		if inlist("`z'","LOO_B","RSS_B") {
			local p=r(mean)
			if `p'<0.01 local stars "***"
			else if `p'<0.05 local stars "**"
			else if `p'<0.1 local stars "*"
			else local stars ""
			file write table "`stars'"
		}
	}
	}
	file write table " \\" _n

	file write table "\hspace{1em} & "
	foreach z in LOO_SE RSS_SE {
		sum `z' if outcome=="`k'" & group==1
		file write table " & (" (string(r(mean),"%15.4fc")) ")"
	}
	file write table " & "
	foreach z in LOO_SE RSS_SE {
		sum `z' if outcome=="`k'" & group==2
		file write table " & (" (string(r(mean),"%15.4fc")) ")"
	}
	file write table " \\" _n
}

file write table "\addlinespace[0.3em]" _n "\multicolumn{8}{l}{\textbf{Primary Outcome Components, Counts}}\\" _n
foreach k in vshoot ashoot aother {
	if "`k'"=="vshoot" local name "Shooting \& Homicide Victimizations"
	if "`k'"=="ashoot" local name "Shooting \& Homicide Arrests"
	if "`k'"=="aother" local name "Other Serious Violent-Crime Arrests"

	file write table "\hspace{1em}`name'"
	foreach g in 1 2 {
	foreach z in CM LOO_B RSS_B {
		sum `z' if outcome=="`k'" & group==`g'
		file write table " & " (string(r(mean),"%15.4fc"))
		if "`z'"=="LOO_B" sum LOO_p if outcome=="`k'" & group==`g'
		if "`z'"=="RSS_B" sum RSS_p if outcome=="`k'" & group==`g'
		if inlist("`z'","LOO_B","RSS_B") {
			local p=r(mean)
			if `p'<0.01 local stars "***"
			else if `p'<0.05 local stars "**"
			else if `p'<0.1 local stars "*"
			else local stars ""
			file write table "`stars'"
		}
	}
	}
	file write table " \\" _n

	file write table "\hspace{1em} & "
	foreach z in LOO_SE RSS_SE {
		sum `z' if outcome=="`k'" & group==1
		file write table " & (" (string(r(mean),"%15.4fc")) ")"
	}
	file write table " & "
	foreach z in LOO_SE RSS_SE {
		sum `z' if outcome=="`k'" & group==2
		file write table " & (" (string(r(mean),"%15.4fc")) ")"
	}
	file write table " \\" _n
}
file write table "\midrule" _n

file write table "\hspace{1em}Take-up rate & "
sum var if outcome=="takeup" & group==1
file write table " & " (string(r(mean),"%15.0fc")) "\% & & "
sum var if outcome=="takeup" & group==2
file write table " & " (string(r(mean),"%15.0fc")) "\% & \\" _n

file write table "\hspace{1em}Average total hours & "
sum var if outcome=="dosage" & group==1
file write table " & " (string(r(mean),"%15.0fc")) " & & "
sum var if outcome=="dosage" & group==2
file write table " & " (string(r(mean),"%15.0fc")) " & \\" _n

file write table "\hspace{1em}N & "
sum var if outcome=="N" & group==1
file write table " & " (string(r(mean),"%15.0gc")) " & & "
sum var if outcome=="N" & group==2
file write table " & " (string(r(mean),"%15.0gc")) " & \\" _n

file close table

